package com.coder.bike.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.bike.entity.Payment;
import com.coder.bike.service.IPaymentService;
import com.coder.bike.utils.Result;
import com.coder.bike.vo.PaymentRequest;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/bike/payment")
public class PaymentController {

    private final IPaymentService paymentService;

    public PaymentController(IPaymentService paymentService) {
        this.paymentService = paymentService;
    }

    @PostMapping("/create")
    public Result createPayment(
            @RequestBody PaymentRequest request) {
        try {
            return paymentService.createPayment(request.getCustomerId(), request.getAmount(), request.getSubject());
        } catch (Exception e) {
            return Result.fail().setMessage("创建支付订单失败: " + e.getMessage());
        }
    }



    @GetMapping("/query/{paymentNo}")
    public Result queryPayment(@PathVariable String paymentNo) {
        try {
            return paymentService.queryPayment(paymentNo);
        } catch (Exception e) {
            return Result.fail().setMessage("查询支付状态失败: " + e.getMessage());
        }
    }

    @PostMapping("/mock-success/{paymentNo}")
    public Result mockPaymentSuccess(@PathVariable String paymentNo) {
        try {
            return paymentService.mockPaymentSuccess(paymentNo);
        } catch (Exception e) {
            return Result.fail().setMessage("模拟支付失败: " + e.getMessage());
        }
    }

    @GetMapping("/qr-code-url")
    public Result getQrCodeUrl() {
        return Result.success("https://sharing-bike.oss-cn-beijing.aliyuncs.com/c33e8a6ab2e0bc30c7e276eaf5cc0e5.jpg");
    }

    @GetMapping("/records")
    public Result getPaymentRecords(
            @RequestParam Integer customerId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<Payment> page = new Page<>(current, size);
            return paymentService.getPaymentRecords(page, customerId);
        } catch (Exception e) {
            return Result.fail().setMessage("获取支付记录失败: " + e.getMessage());
        }
    }
}